package com.qj.platform.pay.core.dao;

import com.qj.base.commons.domain.SearchObject;
import com.qj.base.commons.utils.CodeGenerator;
import com.qj.base.commons.utils.SqlUtil;
import com.qj.platform.pay.mssm.domain.Merchant;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhijie on 2016/12/14.
 */
@Repository
public class MerchantDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public Merchant getById(String id){
        List<Merchant> merchantList=jdbcTemplate.query("select * from merchant where id=? or merchant_code=? limit 1",new Object[]{id,id},new MerchantRowMapper());
        if(merchantList.size()>0){
            return merchantList.get(0);
        }
        return null;
    }

    public String insert(Merchant merchant){
        StringBuilder sql=new StringBuilder("insert into merchant(");
        StringBuilder values=new StringBuilder();
        List<Object> argList=new ArrayList<Object>();
        if(StringUtils.isNotEmpty(merchant.getAccName())){
            argList.add(merchant.getAccName());
            sql.append("acc_name,");
            values.append("?,");
        }
        if(StringUtils.isNotEmpty(merchant.getPayWay())){
            argList.add(merchant.getPayWay());
            sql.append("pay_way,");
            values.append("?,");
        }
        if(StringUtils.isNotEmpty(merchant.getMerchantName())){
            argList.add(merchant.getMerchantName());
            sql.append("merchant_name,");
            values.append("?,");
        }
        if(StringUtils.isNotEmpty(merchant.getAccNo())){
            argList.add(merchant.getAccNo());
            sql.append("acc_no,");
            values.append("?,");
        }
        if(StringUtils.isNotEmpty(merchant.getShortName())){
            argList.add(merchant.getShortName());
            sql.append("short_name,");
            values.append("?,");
        }
        if(StringUtils.isNotEmpty(merchant.getMerchantAddress())){
            argList.add(merchant.getAccName());
            sql.append("merchant_address,");
            values.append("?,");
        }
        if(StringUtils.isNotEmpty(merchant.getServicePhone())){
            argList.add(merchant.getServicePhone());
            sql.append("service_phone,");
            values.append("?,");
        }
        if(StringUtils.isNotEmpty(merchant.getCategory())){
            argList.add(merchant.getCategory());
            sql.append("category,");
            values.append("?,");
        }
        if(StringUtils.isNotEmpty(merchant.getMerchantCode())){
            argList.add(merchant.getMerchantCode());
            sql.append("merchant_code,");
            values.append("?,");
        }
        if(StringUtils.isNotEmpty(merchant.getMerchantLicense())){
            argList.add(merchant.getMerchantLicense());
            sql.append("merchant_license,");
            values.append("?,");
        }
        if(StringUtils.isNotEmpty(merchant.getIdCard())){
            argList.add(merchant.getIdCard());
            sql.append("id_card,");
            values.append("?,");
        }
        if(StringUtils.isNotEmpty(merchant.getRemark())){
            argList.add(merchant.getRemark());
            sql.append("remark,");
            values.append("?,");
        }


        argList.add(merchant.getT0drawFee());
        sql.append("t0_draw_fee,");
        values.append("?,");
        argList.add(merchant.getT0tradeRate());
        sql.append("t0_trade_rate,");
        values.append("?,");
        argList.add(merchant.getT1drawFee());
        sql.append("t1_draw_fee,");
        values.append("?,");
        argList.add(merchant.getT1tradeRate());
        sql.append("t1_trade_rate,");
        values.append("?,");


        sql.append("create_time,");
        values.append("now(),");
        sql.append("modify_time,");
        values.append("now(),");

        merchant.setMerchantId(CodeGenerator.getUUID32());
        argList.add(merchant.getMerchantId());
        sql.append("id");
        values.append("?");

        sql.append(") values(").append(values).append(")");
        jdbcTemplate.update(sql.toString(),argList.toArray(new Object[]{}));
        return merchant.getMerchantId();
    }

    public void update(Merchant merchant){
        List<Object> argList=new ArrayList<>();
        StringBuilder sql=new StringBuilder("update merchant set modify_time=now()");

        if(StringUtils.isNotEmpty(merchant.getAccName())){
            argList.add(merchant.getAccName());
            sql.append(",acc_name=?");
        }
        if(StringUtils.isNotEmpty(merchant.getPayWay())){
            argList.add(merchant.getPayWay());
            sql.append(",pay_way=?");
        }
        if(StringUtils.isNotEmpty(merchant.getMerchantName())){
            argList.add(merchant.getMerchantName());
            sql.append(",merchant_name=?");
        }
        if(StringUtils.isNotEmpty(merchant.getAccNo())){
            argList.add(merchant.getAccNo());
            sql.append(",acc_no=?");
        }
        if(StringUtils.isNotEmpty(merchant.getShortName())){
            argList.add(merchant.getShortName());
            sql.append(",short_name=?");
        }
        if(StringUtils.isNotEmpty(merchant.getMerchantAddress())){
            argList.add(merchant.getAccName());
            sql.append(",merchant_address=?");
        }
        if(StringUtils.isNotEmpty(merchant.getServicePhone())){
            argList.add(merchant.getServicePhone());
            sql.append(",service_phone=?");
        }
        if(StringUtils.isNotEmpty(merchant.getCategory())){
            argList.add(merchant.getCategory());
            sql.append(",category=?");
        }
        if(StringUtils.isNotEmpty(merchant.getMerchantCode())){
            argList.add(merchant.getMerchantCode());
            sql.append(",merchant_code=?");
        }
        if(StringUtils.isNotEmpty(merchant.getMerchantLicense())){
            argList.add(merchant.getMerchantLicense());
            sql.append(",merchant_license=?");
        }
        if(StringUtils.isNotEmpty(merchant.getIdCard())){
            argList.add(merchant.getIdCard());
            sql.append(",id_card=?");
        }
        if(StringUtils.isNotEmpty(merchant.getRemark())){
            argList.add(merchant.getRemark());
            sql.append(",remark=?");
        }
        if(StringUtils.isNotEmpty(merchant.getMerchantCode())){
            argList.add(merchant.getMerchantCode());
            sql.append(",merchant_code=?");
        }
        if(merchant.getT0drawFee()>=0) {
            argList.add(merchant.getT0drawFee());
            sql.append(",t0_draw_fee=?");
        }
        if(merchant.getT0tradeRate()>=0) {
            argList.add(merchant.getT0tradeRate());
            sql.append(",t0_trade_rate=?");
        }
        if(merchant.getT1drawFee()>=0) {
            argList.add(merchant.getT1drawFee());
            sql.append(",t1_draw_fee=?");
        }
        if(merchant.getT1drawFee()>=0) {
            argList.add(merchant.getT1tradeRate());
            sql.append(",t1_trade_rate=?");
        }
        sql.append(" where id=?");
        argList.add(merchant.getMerchantId());
        jdbcTemplate.update(sql.toString(),argList.toArray(new Object[]{}));
    }

    public void updateMerchantCode(String merchantId,String merchantCode){
        List<Object> argList=new ArrayList<>();
        StringBuilder sql=new StringBuilder("update merchant set modify_time=now");
        argList.add(merchantCode);
        sql.append(",merchant_code=?");
        sql.append(" where id=?");
        argList.add(merchantId);
        jdbcTemplate.update(sql.toString(),argList.toArray(new Object[]{}));
    }

    public List<Merchant> search(SearchObject searchObject){
        List<Object> argList=new ArrayList<>();
        return jdbcTemplate.query(SqlUtil.getSearchSql(argList,"merchant",searchObject),argList.toArray(new Object[]{}),new MerchantRowMapper());
    }
    public int searchCount(SearchObject searchObject){
        List<Object> argList=new ArrayList<>();
        return jdbcTemplate.queryForObject(SqlUtil.getCountSql(argList,"merchant",searchObject),argList.toArray(new Object[]{}),Integer.class);
    }

    class MerchantRowMapper implements RowMapper<Merchant>{
        @Override
        public Merchant mapRow(ResultSet resultSet, int i) throws SQLException {
            Merchant merchant=new Merchant();
            merchant.setMerchantId(resultSet.getString("id"));
            merchant.setAccNo(resultSet.getString("acc_no"));
            merchant.setCreateTime(resultSet.getTimestamp("create_time"));
            merchant.setModifyTime(resultSet.getTimestamp("modify_time"));
            merchant.setShortName(resultSet.getString("short_name"));
            merchant.setAccName(resultSet.getString("acc_name"));
            merchant.setRemark(resultSet.getString("remark"));
            merchant.setUserId(resultSet.getString("user_id"));
            merchant.setAgentId(resultSet.getString("agent_id"));
            merchant.setBankName(resultSet.getString("bank_name"));
            merchant.setBankType(resultSet.getString("bank_type"));
            merchant.setIdCard(resultSet.getString("id_card"));
            merchant.setT0tradeRate(resultSet.getDouble("t0_trade_rate"));
            merchant.setT1tradeRate(resultSet.getDouble("t1_trade_rate"));
            merchant.setT0drawFee(resultSet.getDouble("t0_draw_fee"));
            merchant.setT1drawFee(resultSet.getDouble("t1_draw_fee"));
            merchant.setMerchantCode(resultSet.getString("merchant_code"));
            merchant.setMerchantName(resultSet.getString("merchant_name"));
            merchant.setChannelMerchantCode(resultSet.getString("channel_merchant_code"));
            merchant.setCategory(resultSet.getString("category"));
            merchant.setServicePhone(resultSet.getString("service_phone"));
            merchant.setMerchantAddress(resultSet.getString("merchant_address"));

            return merchant;
        }
    }

}
